#!/bin/sh

uci -q batch <<-EOF >/dev/null
	delete ucitrack.@passwall[-1]
	add ucitrack passwall
	set ucitrack.@passwall[-1].init=passwall
	commit ucitrack
	delete firewall.passwall
	set firewall.passwall=include
	set firewall.passwall.type=script
	set firewall.passwall.path=/var/etc/passwall.include
	set firewall.passwall.reload=1
	commit firewall
	delete ucitrack.@passwall_server[-1]
	add ucitrack passwall_server
	set ucitrack.@passwall_server[-1].init=passwall_server
	commit ucitrack
	delete firewall.passwall_server
	set firewall.passwall_server=include
	set firewall.passwall_server.type=script
	set firewall.passwall_server.path=/var/etc/passwall_server.include
	set firewall.passwall_server.reload=1
	commit firewall
	set uhttpd.main.max_requests=50
	commit uhttpd
EOF

tmp=$(uci -q get passwall.@global_app[0].xray_file)
if [ -z "$tmp" ]; then
	uci set passwall.@global_app[0].xray_file="/usr/bin/xray"
else
	[ -n "$(echo $tmp | grep -E "^.*/xray/$")" ] && {
		uci set passwall.@global_app[0].xray_file="${tmp%?}"
	}
fi
tmp=$(uci -q get passwall.@global_app[0].v2ray_file)
if [ -z "$tmp" ]; then
	uci set passwall.@global_app[0].v2ray_file="/usr/bin/v2ray"
else
	[ -n "$(echo $tmp | grep -E "^.*/v2ray/$")" ] && {
		uci set passwall.@global_app[0].v2ray_file="${tmp%?}"
	}
fi
uci commit passwall

/etc/init.d/https-dns-proxy stop >/dev/null 2>&1 &
/etc/init.d/https-dns-proxy disable >/dev/null 2>&1 &
rm -rf /tmp/luci-*cache
exit 0
